1. /* sifismul.cpp by K.Tsuru */
  2. // function ID = 424 BRADIX
  3. /*****************************************
  4. SInteger class
  5. It multiplies SInteger 'm' by ulong integer 'x'.
  6. result = m*x for x < ULONG_MAX
  7. ******************************************/
  8. #ifndef SN_H
  9. #include "sn.h"
  10. #endif
  11. void IsMult(const SInteger& m, ulong x, SInteger& result){
  12. if(!m.Sign(424) || x == 1UL){ result = m; return; }
  13. if(x == 0){ result.SetZero(); return; }
  14. const ulong mt = m.SlOpMaxValue();
  15. if(x > mt){
  16. SInteger s(x);
  17. result = NLLMult(m, s);
  18. return;
  19. // m.SetError(m.OUT_OF_RANGE,"IsMult", 424);
  20. }
  21. uint rh = m.Head(), rt = m.Tail();
  22. if( &m != &result ){
  23. //When &m == &result do not execute/not need.
  24. if(result.Size() < rh+1u) result.valloc(rh+1u, -1);
  25. //It fills the not substituted part with zero.
  26. if(rt) result.figure.clear(result.aTail, rt-1u);
  27. result.figure.clear(rh+1u);
  28. }
  29. const fType* mv = m.ReadFigures();
  30. fType* rv = result.figure.Elements();
  31. #ifndef NDEBUG
  32. result.figure(rh); m.figure(rh);
  33. #endif
  34. register uint i;
  35. ulong t = 0, rdx1 = (ulong)BRADIX1;
  36. for(i = rt; i <= rh; i++) {
  37. t += (ulong)mv[i]*x;
  38. rv[i] = fType(t & rdx1);
  39. t >>= BRADIX_BITS;
  40. }
  41. if(t){//carry to the top
  42. if(t < BRADIX){
  43. rh++;
  44. result.Reserve(rh); // result.size() >= rh+i+1
  45. result.figure[rh] = (fType)t;
  46. rv = result.figure.Elements();
  47. } else {//It normalizes the carry and copy to the top.
  48. fType temp[5];
  49. i = 0;
  50. while(t){
  51. temp[i] = fType(t & rdx1);
  52. t >>= BRADIX_BITS; i++;
  53. }
  54. result.Reserve(rh+i); // result.size() >= rh+i+1
  55. rv = result.figure.Elements();
  56. memcpy( (rv+rh+1u), temp, i*sizeof(fType));
  57. rh += i;
  58. }
  59. }
  60. result.aHead = rh;
  61. while( !rv[rt] ) rt++;
  62. result.aTail = rt;
  63. #ifndef NDEBUG
  64. result.figure(rh);
  65. #endif
  66. result.SetSign(m.RawSign());
  67. if( 2u*(result.aHead+1) <= result.figure.size() ) result.DoCutDown();
  68. }

sifismul.cpp : last modifiled at 2017/03/13 14:31:59(1,933 bytes)
created at 2016/04/25 14:53:17
The creation time of this html file is 2017/10/25 11:09:45 (Wed Oct 25 11:09:45 2017).